package com.example.customer.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.PageUtils;
import com.example.common.dto.BaseReturnDto;
import com.example.customer.entity.CustomerDO;
import com.example.customer.mapper.CustomerMapper;
import com.example.customer.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.redis.RedisCache;
import com.example.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.sql.Wrapper;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tc
 * @since 2022-10-28
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerDO> implements CustomerService {
    @Autowired
    RedisUtils redisUtils;
//    RedisTemplate redisTemplate;
    RedisCache redisCache;
    @Override
    public PageUtils Querylist(Integer pageNum, Integer pageSize, String search) {
        //TODO 设置key表名+页数+收缩关键词
        String redisKey = "Customer"+":"+pageNum+":"+search;
        System.out.println(redisKey);
        boolean  i =redisUtils.hasKey(redisKey);
        List<CustomerDO> list = baseMapper.selectList(null);
        Page<CustomerDO> page =new Page<>();
        if(i==false){
            Page<CustomerDO> page1 = baseMapper.selectPage(new Page<>(pageNum,pageSize), Wrappers.<CustomerDO>lambdaQuery().like(CustomerDO::getName,search));
            //JSON
            String arrJson = JSON.toJSONString(page1.getRecords());
            this.redisUtils.setCacheObject(redisKey,arrJson);
            int total =list.size();
            page1.setTotal(total);
            return new PageUtils(page1);
        }else{
            Object list1 = redisUtils.get(redisKey);
            List<CustomerDO> list2 = JSONObject.parseArray((String) list1,CustomerDO.class);
            page.setRecords(list2);
            int total =list.size();
            page.setTotal(total);
            return new PageUtils(page);
        }
    }

    @Override
    public BaseReturnDto addCustomer(String name, String sex, String pwd, String address, String email) {
        try{
            if("".equals(address)||address==null){
                throw new IllegalArgumentException("地址不能为空！");
            }
            CustomerDO customer = new CustomerDO();
            customer.setName(name);
            customer.setSex(sex);
            customer.setPwd(pwd);
            customer.setAddress(address);
            customer.setEmail(email);
            this.baseMapper.insert(customer);
            return BaseReturnDto.success();
        }catch (Exception e){
            BaseReturnDto baseReturnDto = new BaseReturnDto();
            baseReturnDto.setCode(202);
            baseReturnDto.setMsg(e.getMessage());
            e.printStackTrace();
            return baseReturnDto;
        }
    }

    @Override
    public BaseReturnDto exportCustomer(HttpServletRequest req, HttpServletResponse resp) {
        try{
            resp.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("顾客信息导出", "UTF-8");
            resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            resp.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            List<CustomerDO> customerList = this.baseMapper.selectAll();
            EasyExcel.write(resp.getOutputStream(), CustomerDO.class).sheet("sheet1").doWrite(customerList);
            return BaseReturnDto.success();
        }catch (Exception e){
            BaseReturnDto baseReturnDto = new BaseReturnDto();
            baseReturnDto.setCode(202);
            baseReturnDto.setMsg(e.getMessage());
            e.printStackTrace();
            return baseReturnDto;
        }
    }


}
